<template>
    <div class="cms-catalog">
        <div class="cms-catalog-site">
            <el-table
                    @row-click="handleSiteRowClick"
                    border
                    default-expand-all
                    highlight-current-row
                    :data="siteTable"
                    row-key="id"
                    height="500"
                    size="mini"
                    style="width: 100%"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="site"
                        width="*">
                </el-table-column>
            </el-table>
        </div>
        <div class="cms-catalog-table">
            <div style="height: 35px">
                <el-form :model="searchForm" :inline="true" size="mini">
                    <el-form-item label="name">
                        <el-input v-model="searchForm.name"></el-input>
                    </el-form-item>
                    <el-form-item label="code">
                        <el-input v-model="searchForm.code"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="listCatalog">Search</el-button>
                        <el-button type="primary" @click="listCatalogTree">Refresh</el-button>
                        <el-button type="primary" @click="handleCreate('0')">New</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table
                    border
                    default-expand-all
                    highlight-current-row
                    :data="catalogTable"
                    row-key="ukId"
                    height="500"
                    size="mini"
                    style="width: 100%"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="name"
                        width="*">
                </el-table-column>
                <el-table-column
                        prop="code"
                        label="code"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="status"
                        width="70">
                    <template slot-scope="scope">
                        <el-switch
                                @change="statusChange($event, scope.row.ukId)"
                                :value="scope.row.status != '1'"
                                active-color="#909399"
                                inactive-color="#67c23a">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="sort"
                        label="sort"
                        width="60">
                </el-table-column>
                <el-table-column
                        label="操作"
                        width="235">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleCreate(scope.row.ukId)">新建下级
                        </el-button>
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.row.ukId)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog title="栏目管理" :visible.sync="showCatalogForm" :close-on-click-modal="false" label-width="80px">
            <el-form ref="form" :model="catalogForm" label-width="80px">
                <el-form-item label="所属站点">
                    <el-select v-model="catalogForm.siteId" placeholder="请选择站点">
                        <el-option
                                v-for="item in this.siteTable"
                                :key="item.ukId"
                                :label="item.name"
                                :value="item.ukId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="栏目编码">
                    <el-input v-model="catalogForm.code"></el-input>
                </el-form-item>
                <el-form-item label="栏目名称">
                    <el-input v-model="catalogForm.name"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="catalogForm.status" placeholder="请选择状态">
                        <el-option
                                v-for="item in this.$store.state.Pabu.dict.manage.STATUS.dictItems"
                                :key="item.ukId"
                                :label="item.name"
                                :value="parseInt(item.value)">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="catalogForm.sort" type="number" min="0" max="90000"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSave">保存</el-button>
                    <el-button @click="showCatalogForm = false">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
    import {
        deleteCatalog,
        listCatalog,
        listCatalogTree,
        saveCatalog,
        updateCatalogStatus
    } from "../../../api/cms/catalog";
    import {listSite} from "../../../api/cms/site";

    export default {
        name: "Catalog",
        data() {
            return {
                showCatalogForm: false,
                showCatalogDetails: false,
                siteTable: [],
                catalogTable: [],
                searchForm: {},
                catalogForm: {
                    catalogInfo: {}
                },
            }
        },
        methods: {
            handleCreate(parentId) {
                this.catalogForm = {
                    siteId: this.catalogForm.siteId,
                    parentId: parentId,
                    status: 1,
                    catalogInfo: {}
                }
                this.showCatalogForm = true
            },
            handleEdit(row) {
                this.catalogForm = row
                this.showCatalogForm = true
            },
            handleSave() {
                saveCatalog(this.catalogForm).then(result => {
                    this.listCatalogTree()
                    this.showCatalogForm = false
                }).catch(error => {
                    console.log(error)
                })
            },
            handleDelete(ukId) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteCatalog(ukId).then(result => {
                        this.listCatalogTree()
                        this.showCatalogForm = false
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }).catch(error => {
                        console.log(error)
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            statusChange(value, catalogId) {
                let status = value ? 0 : 1
                updateCatalogStatus(catalogId, status).then(result => {
                    this.listCatalogTree()
                }).catch(error => {
                    console.log(error)
                })
            },
            handleSiteRowClick(row) {
                this.catalogForm.siteId = row.ukId
                this.searchForm.siteId = row.ukId
                this.listCatalogTree()
            },
            listCatalog() {
                this.searchForm = {
                    "name": this.searchForm.name
                }
                listCatalog(this.searchForm).then(result => {
                    this.catalogTable = result.data.catalogs.records
                }).catch(error => {
                    console.log(error)
                })
            },
            listSite() {
                listSite({}).then(result => {
                    this.siteTable = result.data.sites
                }).catch(error => {
                    console.log(error)
                })
            },
            listCatalogTree() {
                this.searchForm.parentId = "0"
                listCatalogTree(this.searchForm).then(result => {
                    this.catalogTable = result.data.catalogTree
                }).catch(error => {
                    console.log(error)
                })
            }
        },
        created() {
            this.listSite()
        }
    }
</script>

<style>
    .cms-catalog {
        width: 100%;
        height: 100%;
    }

    .cms-catalog-site {
        width: 300px;
        height: 100%;
        float: left;
    }

    .cms-catalog-table {
        padding-left: 5px;
        width: calc(100% - 305px);
        height: calc(100% - 35px);
        float: left;
    }

    /* element ui样式重写 */
    .cms-catalog > .cms-catalog-site > .el-table,
    .cms-catalog > .cms-catalog-table > .el-table {
        height: calc(100% - 5px) !important;
    }

    .cms-catalog > .cms-catalog-table > .el-table > .el-table__body-wrapper {
        height: calc(100% - 5px) !important;
    }

</style>
